package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserCountService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据统计
 *
 * @author DogeGuo
 */
@Component
public class UserCountsManager {

    @DubboReference
    UserCountService userCountService;

    /**
     * 喜欢，粉丝，互相喜欢数量
     *
     * @return
     */
    public ResponseEntity userLikeEachOther() {
        //获得当前用户的id
        Long userId = UserHolder.get().getId();

        //调用UserCountService查询
        Map map = userCountService.userLikeEachOther(userId);

        return ResponseEntity.ok(map);
    }

    @DubboReference
    private UserInfoService userInfoService;

    /**
     * 查询互相喜欢、喜欢、粉丝详情
     *
     * @param typeStr
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResponseEntity LikeDetails(String typeStr, Integer pageNum, Integer pageSize) {
        //获得当前用户的id
        Long userId = UserHolder.get().getId();

        //type转int
        int type = Integer.parseInt(typeStr);

        //调用UserCountService方法
        PageBeanVo pageBeanVo = userCountService.likeDetails(type, userId, pageNum, pageSize);

        List<UserLikeVo> userList = new ArrayList<>();
        //获得看过我的游客详情
        if (type == 4) {
            List<Visitor> items = (List<Visitor>) pageBeanVo.getItems();
            if (CollectionUtil.isNotEmpty(items)) {
                for (Visitor item : items) {
                    //获得看过我的游客信息详情
                    UserInfo visitorInfo = userInfoService.findById(item.getVisitorUserId());

                    //封装userLikeVo
                    UserLikeVo userLikeVo = new UserLikeVo();
                    //游客id
                    userLikeVo.setId(visitorInfo.getId());
                    //头像
                    userLikeVo.setAvatar(visitorInfo.getAvatar());
                    //昵称
                    userLikeVo.setNickname(visitorInfo.getNickname());
                    //性别
                    userLikeVo.setGender(visitorInfo.getGender());
                    //年龄
                    userLikeVo.setAge(visitorInfo.getAge());
                    //城市
                    userLikeVo.setCity(visitorInfo.getCity());
                    //学历
                    userLikeVo.setEducation(visitorInfo.getEducation());
                    //婚姻状况
                    userLikeVo.setMarriage(visitorInfo.getMarriage());
                    //匹配度
                    userLikeVo.setMatchRate(RandomUtil.randomInt(33, 99));
                    //是否喜欢，默认不喜欢
                    userLikeVo.setAlreadyLove(false);

                    userList.add(userLikeVo);
                }

            }
        } else {
            //获取喜欢列表里的用户详情
            List<Long> idList = (List<Long>) pageBeanVo.getItems();
            if (CollectionUtil.isNotEmpty(idList)) {
                for (Long id : idList) {
                    //获取喜欢列表里的用户详情信息
                    UserInfo userInfo = userInfoService.findById(id);

                    //封装userLikeVo
                    UserLikeVo userLikeVo = new UserLikeVo();
                    //用户的id
                    userLikeVo.setId(userInfo.getId());
                    //头像
                    userLikeVo.setAvatar(userInfo.getAvatar());
                    //昵称
                    userLikeVo.setNickname(userInfo.getNickname());
                    //性别
                    userLikeVo.setGender(userInfo.getGender());
                    //年龄
                    userLikeVo.setAge(userInfo.getAge());
                    //城市
                    userLikeVo.setCity(userInfo.getCity());
                    //学历
                    userLikeVo.setEducation(userInfo.getEducation());
                    //婚姻状况
                    userLikeVo.setMarriage(userInfo.getMarriage());
                    //匹配度
                    userLikeVo.setMatchRate(RandomUtil.randomInt(33, 99));
                    //是否喜欢，默认不喜欢
                    userLikeVo.setAlreadyLove(false);

                    userList.add(userLikeVo);
                }

            }
        }
        //将userList封装到pageBeanVo
        pageBeanVo.setItems(userList);

        return ResponseEntity.ok(pageBeanVo);
    }
}
